package com.jincp.filepicker.base;

import com.jincp.filepicker.bean.BaseFile;
import com.jincp.filepicker.bean.Directory;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：图片选择的入口类
 * 修订历史：
 * ================================================
 */
public class BasePickerManager<T extends BasePickerManager> extends Observable{

    public static final String EXTRA_RESULT_ITEMS = "EXTRA_RESULT_ITEMS";
    public static final String EXTRA_RESULT_ITEM = "EXTRA_RESULT_ITEM";

    protected boolean isMultiMode = false;    //图片选择模式
    protected int maxSelectedLimit = 9;         //最大选择图片数量

    protected boolean isCleanSelectedItem = false;    //清除图片选择

    protected int currentDirectoryIndex = 0;  //当前选中的文件夹位置 0表示所有图片

    public T setMultiMode(boolean multiMode) {
        this.isMultiMode = multiMode;
        return (T)this;
    }

    public T setMaxSelectedLimit(int maxSelectedLimit) {
        this.maxSelectedLimit = maxSelectedLimit;
        return (T)this;
    }

    public T setCleanSelectedItem(boolean isCleanSelectedItem) {
        this.isCleanSelectedItem = isCleanSelectedItem;
        return (T)this;
    }

    public boolean isMultiMode() {
        return isMultiMode;
    }

    public boolean isCleanSelectedItem() {
        return isCleanSelectedItem;
    }

    public int getMaxSelectedLimit() {
        return maxSelectedLimit;
    }

    private List directoryList;

    public List getDirectoryList() {
        return directoryList;
    }

    public void setDirectoryList(List directoryList) {
        this.directoryList = directoryList;
    }

    public void setCurrentDirectoryIndex(int currentDirectoryIndex) {
        this.currentDirectoryIndex = currentDirectoryIndex;
    }

    public int getCurrentDirectoryIndex() {
        return currentDirectoryIndex;
    }

    public List getCurrentDirectoryFileList() {
        Directory directory = (Directory) directoryList.get(currentDirectoryIndex);
        return directory.getFiles();
    }

    private List<BaseFile> selectedFileList;   //选中的图片集合

    public List<BaseFile> getSelectedFileList() {
        return selectedFileList;
    }

    public void setSelectedFileList(List<BaseFile> selectedFileList) {
        this.selectedFileList = selectedFileList;
    }

    public void addSelectedFile(BaseFile file) {
        if (selectedFileList == null){
            selectedFileList = new ArrayList<>();
        }
        if (!isContainSelected(file)){
            selectedFileList.add(file);
            setChanged();
            notifyObservers("Add");
        }
    }

    public void removeSelectedFile(BaseFile file) {
        if (selectedFileList == null){
            selectedFileList = new ArrayList<>();
        }
        if (isContainSelected(file)){
            selectedFileList.remove(file);
            setChanged();
            notifyObservers("Remove");
        }
    }

    public void clear() {
        if (selectedFileList != null) {
            selectedFileList.clear();
            setChanged();
            notifyObservers("Clean");
        }
        currentDirectoryIndex = 0;
    }

    public void completedSelector() {
            setChanged();
            notifyObservers("Completed");
    }

    public boolean isContainSelected(BaseFile file) {
        if (selectedFileList == null) {
            return false;
        }
        return selectedFileList.contains(file);
    }

    public int getSelectFileCount() {
        if (selectedFileList == null) {
            return 0;
        }
        return selectedFileList.size();
    }

}